goog.provide('dkpoints.nav.NotAuthorizedHandler');

goog.require('dkpoints.errors.templates');

/**
 * @fileoverview
 * Equivalent of HTTP 403 error - Forbidden.  Denotes that the user is not
 * authorized to view the contents at the path he has navigated to.
 */



/**
 * Handler for showing an error when the user navigates to a URL which they
 * are not authorized to see.
 * @param {dkpoints.ServiceProvider} sp The ServiceProvider object with all the
 *    cool dependencies baked in.
 * @constructor
 */
dkpoints.nav.NotAuthorizedHandler = function(sp) {
  /**
   * @type {dkpoints.auth.Auth}
   * @private
   */
  this.auth_ = sp.getAuth();

  /**
   * @type {!Element}
   * @private
   */
  this.bodyContent_ = sp.getBodyContent();
};


/**
 * The auth level required for this module.
 * @type {dkpoints.auth.AuthState}
 * @const
 */
dkpoints.nav.NotAuthorizedHandler.requiredAuth =
    dkpoints.auth.AuthState.NO_AUTH;


/**
 * The call to action for a navigation handler.
 * @param {dkpoints.nav.Path} path The path for this request.
 */
dkpoints.nav.NotAuthorizedHandler.prototype.handle = function(path) {
  var callback = goog.bind(this.showAuthError_, this, path);
  this.auth_.getAuthStatus(callback);
};


/**
 * Got the user's auth status from the auth manager.  Display the appropriate
 * error.
 * @param {dkpoints.nav.Path} path The path object for this navigation.
 * @param {dkpoints.rpc.commands.AuthCheckResponse} authStatus The user's
 *    current auth status.
 * @private
 */
dkpoints.nav.NotAuthorizedHandler.prototype.showAuthError_ =
    function(path, authStatus) {

  var params = {
    authState: authStatus.getState(),
    requiredAuth: path.requiredAuth
  };
  this.bodyContent_.innerHTML = dkpoints.errors.templates.notAuthorized(params);
};


/**
 * When the handler instance is already active, transition will be called on
 * new navigations instead of handle to allow the handler to more easily
 * transition set up the new state of the active driver instead of rebuilding
 * it from scratch.
 * @param {dkpoints.nav.Path} path An object with request-specific parameters.
 * @param {function()} onCancel Callback to be invoked if user decides
 *    not to navigate away from what they're doing.
 */
dkpoints.nav.NotAuthorizedHandler.prototype.transition =
    function(path, onCancel) {
  this.handle(path);
};


/**
 * Save state and clear the user-interface so the next handler can use the
 * screen.
 * @param {function(boolean)} onExit Callback once exit is either
 *    complete or cancelled by the user.  Passing true to the callback means
 *    that the handler has relinquished control of thep age; false means
 *    the user opted not to navigate away.
 */
dkpoints.nav.NotAuthorizedHandler.prototype.exit = function(onExit) {
  this.bodyContent_.innerHTML = '';
  onExit(true); // dispose completed successfully.
};
